from django.http import HttpResponse
from django.http import HttpRequest
from Response.Resp import Resp
import json
from Model.models import User
from Model.models import WordVector
from Model.models import AccessRecords
from utils.worddistince import Distince

from utils.mynet import mynet
import numpy as np

class Access(object):
    @classmethod
    def InfoCollection(self, request: HttpRequest) -> HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                email = json_data['email']
                uid = json_data['uid']
                sex = json_data['sex']
                birth = json_data['birth']
                print(birth, type(birth))
                name = json_data['name']
                major = json_data['major']
                # 从mysql中查询该用户，根据邮箱,保存信息
                now_user = User.objects.get(email=email)
                now_user.name = name
                now_user.major = major
                now_user.bith = birth
                now_user.sex = sex
                now_user.save()

                resp_json_data = {
                    'msg': '修改密码成功',
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)

    @classmethod
    def CreativeCalc(self, request: HttpRequest) -> HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                word_list = json_data['wordlist']
                print(word_list)
                word_vec_list = WordVector.objects.filter(word__in=word_list)
                wlen = len(word_vec_list)
                # start 计算
                sum = 0
                score_matrix = []
                for ix in range(wlen):
                    for jx in range(ix+1, wlen):
                        one_recode = [ ix,jx,
                            int(Distince.CalculateWordDistance(word_vec_list[ix], word_vec_list[jx])*100) ]
                        # print(word_vec_list[ix].word,word_vec_list[jx].word,one_recode[2],sep='\t')
                        sum += one_recode[2]
                        score_matrix.append(one_recode)

                score = sum / (wlen * (wlen-1)/2)
                print('######最后分数###### ', score)

                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.create_score = score
                access_record.save()

                resp_json_data = {
                    'score': int(score),
                    'matrix': score_matrix,
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)
    
    @classmethod
    def AdventureCalc(self, request: HttpRequest) -> HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                score = json_data['score']
                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.adventure_score = score
                access_record.save()

                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)
    
    @classmethod
    def RavenCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                score = json_data['score']
                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.raven_scroe = score
                access_record.save()

                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)

    @classmethod
    def MatchCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                score = json_data['score']
                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.match_score = score
                access_record.save()

                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)

    @classmethod
    def LogicCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                score = json_data['score']
                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.logic_score = score
                access_record.save()
                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)


    @classmethod
    def HanoiCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                step = json_data['step']
                seconds = json_data['seconds']
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.hanoi_step = step
                access_record.hanoi_seconds = seconds
                access_record.save()

                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)

    @classmethod
    def RoomCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                score = json_data['score']
                # 保存到mysql数据库
                user = User.objects.get(id=uid)
                access_record =  AccessRecords.objects.get(
                    uid = user
                )
                access_record.room_score = score
                access_record.save()

                resp_json_data = {
                    'rescode': 200
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)

    def judgeNull(self,val):
        if val==None:
            return 0
        else:
            return val

    @classmethod
    def FinalCalc(self,request:HttpRequest)->HttpResponse:
        try:
            if request.method == 'GET':
                pass
            elif request.method == 'POST':
                json_data = json.loads(request.body)
                uid = json_data['uid']
                ass_all_score = AccessRecords.objects.get(
                    uid=User.objects.get(id=uid)
                )
                t1 = ass_all_score.create_score / 100
                t2 = ass_all_score.adventure_score / 700
                t3 = (20-ass_all_score.hanoi_step)/20
                t4 = (100-ass_all_score.hanoi_seconds)/100
                t5 = (5-ass_all_score.match_score) / 5
                t6 = (4-ass_all_score.raven_scroe) / 4
                t7 = ass_all_score.logic_score / 2
                in_val=np.array([[t1,t2,t3,t4,t5,t6,t7,1,1,1,1]])
                score = mynet(in_val.T)
                print(score)
                # print()
                if score == 0:
                    score = 85
                elif score == 1:
                    score = 70
                elif score == 2:
                    score = 55
                elif score == 3:
                    score = 40
                elif score == 4:
                    score = 35
                ass_all_score.final_score = score
                ass_all_score.save()
                import time
                time.sleep(1)

                resp_json_data = {
                    'rescode': 200,
                }
                return Resp.RightResp(resp_json_data)
        except Exception as e:
            print(repr(e))
            return Resp.ErrorResp({
                'rescode': 400,
                'msg': str(e)
            }, 401)